package com.wavercaz.freeforum.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wavercaz.freeforum.pojo.CommonSearchParam;
import com.wavercaz.freeforum.pojo.Comment;
import com.wavercaz.freeforum.pojo.vo.CommentVo;
import com.wavercaz.freeforum.services.CommentService;
import com.wavercaz.freeforum.services.PostReplyService;
import com.wavercaz.freeforum.utils.QueryUtil;
import com.wavercaz.freeforum.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CommentController {
    @Autowired
    private  CommentService commentService;
    @Autowired
    private PostReplyService postReplyService;

    // 增
    @PutMapping("/comment")
    @Transactional
    public ResponseResult save(@RequestBody Comment comment) {
        boolean result = commentService.save(comment);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "评论失败");
        else return new ResponseResult(HttpStatus.OK.value(), "评论成功");
    }

    // 删
    @DeleteMapping("/comment")
    public ResponseResult delete(@RequestBody List<Integer> ids) {
        boolean result = commentService.removeByIds(ids);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "删除失败");
        else return new ResponseResult(HttpStatus.OK.value(), "删除成功");
    }

    // 改
    @PostMapping("/comment")
    public ResponseResult update(@RequestBody Comment comment) {
        boolean result = commentService.updateById(comment);
        if(!result) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "修改失败");
        else return new ResponseResult(HttpStatus.OK.value(), "修改成功");
    }

    // 查
    @GetMapping("/comment/{id}")
    public ResponseResult get(@PathVariable Integer id ) {
        Comment comment = commentService.getById(id);
        if(comment == null) return new ResponseResult(HttpStatus.NO_CONTENT.value(), "无此相关信息");
        else return new ResponseResult(HttpStatus.OK.value(), comment);
    }

    // 列表
    @PostMapping("/commentList")
    public ResponseResult list(@RequestBody(required = false) CommonSearchParam commonSearchParam) {
        Object data = null;
        if(commonSearchParam != null) {
            QueryWrapper queryWrapper = QueryUtil.getQueryWrapper(commonSearchParam.getSearches());
            if(commonSearchParam.getPageNum()!=null && commonSearchParam.getPageSize()!=null)
                data= commentService.page(new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()), queryWrapper);
            else data= commentService.list(queryWrapper);
        }
        else data= commentService.list();
        return new ResponseResult(HttpStatus.OK.value(), data);
    }

    // vo列表
    @PostMapping("/commentVoList")
    public ResponseResult listVo(@RequestBody CommonSearchParam commonSearchParam) {
        IPage<CommentVo> data = commentService.getCommentVoList(commonSearchParam, new Page<>(commonSearchParam.getPageNum(), commonSearchParam.getPageSize()));
        return new ResponseResult(HttpStatus.OK.value(), data);
    }
}
